<?php

//https://api.hytys.cn/doc/
use think\Response;

function arraySuccess($msg='成功',$data=[]){
    if (!mb_check_encoding($msg, 'UTF-8')) {
        $msg = mb_convert_encoding($msg, 'UTF-8', 'auto');
    }
    return ['code'=>200,'msg'=>$msg,'data' =>$data];
}

function arrayFail($msg='失败',$data=[]){
    if (!mb_check_encoding($msg, 'UTF-8')) {
        $msg = mb_convert_encoding($msg, 'UTF-8', 'auto');
    }
    return ['code'=>500,'msg'=>$msg,'data' =>$data];
}

function jsonSuccess($msg='成功',$data=[]){
    return json_encode(['code'=>200,'msg'=>$msg,'data' =>$data]);
}

function jsonFail($msg='失败',$data=[]){
    return json_encode(['code'=>500,'msg'=>$msg,'data' =>$data]);
}

function result($data = [], string $msg = 'error', int $code = 200, string $type = 'json'):Response {
    $result = [
        "code" => $code,
        "msg" => $msg,
        "data" => $data
    ];
    // 调用Response的create方法，指定code可以改变请求的返回状态码
    return Response::create($result, $type)->code($code);
}


function randomCode(){
    $timestamp = time();
    $randomNumber = rand(10, 99);
    $code = $timestamp . $randomNumber;
    return $code;
}

function getUsername($length = 8) {
    $codeAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $codeAlphabet .= 'abcdefghijklmnopqrstuvwxyz';
    $codeAlphabet .= '0123456789';

    // Shuffle the $codeAlphabet string
    $codeAlphabet = str_shuffle($codeAlphabet);

    // Create an empty string
    $referralCode = '';

    // Build the referral code with the given length
    for ($i = 0; $i < $length; $i++) {
        // Get a random character from the shuffled $codeAlphabet string
        $referralCode .= substr($codeAlphabet, mt_rand(0, strlen($codeAlphabet) - 1), 1);
    }

    return $referralCode;
}

function encrypt($data, $key) {
    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
    $encrypted = openssl_encrypt($data, 'aes-256-cbc', $key, 0, $iv);
    return base64_encode($encrypted . '::' . $iv);
}

function decrypt($data, $key) {
    list($encryptedData, $iv) = explode('::', base64_decode($data), 2);
    return openssl_decrypt($encryptedData, 'aes-256-cbc', $key, 0, $iv);
}

function curlGet($url, $headers = []) {
    $ch = curl_init();

    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_USERAGENT => 'PHP cURL Client',
        CURLOPT_TIMEOUT => 30,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_HTTPHEADER => $headers
    ]);

    $response = curl_exec($ch);
    $error = curl_error($ch);
    $info = curl_getinfo($ch);

    curl_close($ch);

    return [
        'response' => $response,
        'error' => $error,
        'info' => $info
    ];
}

// cURL POST 请求
function curlPost($url, $data = [], $headers = []) {
    $ch = curl_init();

    // 默认添加JSON内容类型头
    $defaultHeaders = ['Content-Type: application/json'];
    $finalHeaders = array_merge($defaultHeaders, $headers);

    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_USERAGENT => 'PHP cURL Client',
        CURLOPT_TIMEOUT => 30,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => is_array($data) ? json_encode($data) : $data,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_HTTPHEADER => $finalHeaders
    ]);

    $response = curl_exec($ch);
    $error = curl_error($ch);
    $info = curl_getinfo($ch);

    curl_close($ch);

    return [
        'response' => $response,
        'error' => $error,
        'info' => $info
    ];
}


